#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/12/6 12:26
# @Author  : tianqunhui
# @contact : tqunhui@qq.com
# @File    : tqh_read_yaml.py
# @Software: PyCharm


import yaml

yaml_list = []
key_temp = ''

def dict_to_list_set(yaml_dict):
  """
  采用递归的方式遍历dict，遇到非字典则将值写入结果。
  """
  global key_temp
  if not isinstance(yaml_dict, dict):
    # 将不是dict时的值作为最终的字符串
    temp_key = key_temp.split('.')
    temp_key.pop()
    temp_key = '.'.join(temp_key)
    re_set = (temp_key, yaml_dict)
    yaml_list.append(re_set)
    # print(re_set)

  else:
    for key, val in yaml_dict.items():
      if key_temp is '':
          key_temp = key + '.'
      else:
          key_temp = key_temp  + key + '.'

      # print(key_temp)
      dict_to_list_set(val)
      key_temp_list = key_temp.split('.')
      key_temp_list.pop()
      key_temp_list.pop()
      key_temp = '.'.join(key_temp_list)+'.'
      # print(key_temp)
      if key_temp is '.':
        key_temp = ''



with open("sample.yaml", 'r') as f:

  config = yaml.load(f, yaml.FullLoader)
  dict_to_list_set(config)


print(yaml_list)



